<template>
	<el-button v-bind="$attrs" @click="handleExportWord()"><slot></slot></el-button>
</template>

<script>
import PizZip from 'pizzip';
import { saveAs } from 'file-saver';
import DocxTemplater from 'docxtemplater';
export default {
	name: 'exportWordButton',
	props: {
		data: { type: Object, default: () => {} }
	},
	data() {
		return {};
	},
	methods: {
		async handleExportWord() {
			try {
				let temp = await fetch('/template.docx');
				let tempData = await temp.arrayBuffer();
				let docObject = new DocxTemplater(new PizZip(tempData), {
					paragraphLoop: true,
					linebreaks: true
				});
				docObject.setData(this.data).render();
				this.exportWordFile(docObject);
			} catch (error) {
				console.error('导出 Word 文档失败:', error);
			}
		},

		//导出文件
		exportWordFile(object) {
			let blob = object.getZip().generate({ type: 'blob' });
			saveAs(blob, `${this.data.docTitle}.docx`);
		}
	}
};
</script>

<style lang="scss" scoped></style>